<form class="layui-form seller-form"  action="" >
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">辅导员：</label>
            <div class="layui-input-inline">
                <select name="userMobile" lay-filter="userMobile" id="userMobile" lay-search>
                    <option value=''>请选择辅导员</option>
                    {volist name="userList" id="user"}
                        <option value="{$user.mobile}">{$user.mobile . ' ' .$user.truename}</option>
                    {/volist}
                </select> 
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">小助手：</label>
            <div class="layui-input-inline">
                <select name="assistantUserMobile" id="assistantUserMobile" lay-search>
                    <option value=''>请选择小助手</option>
                </select> 
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn layui-btn-sm" lay-submit lay-filter="assistantUser-add"><i class="layui-icon">&#xe608;</i>添加</button>
        </div>
    </div>
</form>

<div class="table-body">
    <table id="assistantUserTable" lay-filter="assistantUserTable"></table>
</div>

<script>
    var mgrMobile = '';
    layui.use(['table','form','layer','laydate'],function(){
        var layer = layui.layer, table = layui.table,form = layui.form,date = layui.laydate;
        // 定义变量保存当前页面和当前页面数据条数
        var currPage; 
        var dataNum;

        //执行渲染
        table.render({
            elem: '#assistantUserTable', //指定原始表格元素选择器（推荐id选择器）
            height: '320',
            cellMinWidth: '80',
            page: 'true',
            limit:'20',
            id:'assistantUserTable',
            url: "{:url('BookCommon/returnNull')}",
            cols: [[ //标题栏
                {type:'numbers'},
                {field: 'mobile', title: '小助手手机', width:120, align:'center'}, 
                {field: 'truename', title: '小助手姓名' ,align:'center', width: 120},
                {field: 'operating', title: '操作', width:80, align: 'center', templet:function(data){
                    var html = '';
                    html += '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delAssistantUser">删除</a>';
                    return html;
                }}
            ]], //设置表头
            done: function(res, curr, count){
                // 给变量赋值 
                dataNum = res.data.length;
                currPage = curr;
            }
        });

        form.render();

        date.render({
            elem: '#utime',
            range: '到'
        });

        form.on('submit(assistantUser-add)', function(data){
            if(data.field.userMobile == '') {
                layer.msg('请选择辅导员！');
                return false;
            }

            if(data.field.assistantUserMobile == '') {
                layer.msg('请选择小助手！');
                return false;
            }

            if(data.field.assistantUserMobile == data.field.userMobile) {
                layer.msg('不能委托自己！');
                return false;
            }

            JsPost("{:url('BookAssistant/assistantUserAdd')}", data.field, function(res){
                if(res.status){
                    layer.msg(res.msg,{time:1300},function(){
                        getAssistantInfo(mgrMobile);
                        table.reload('assistantUserTable');
                    });
                }else{
                    layer.msg(res.msg);
                }
            })

            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //监听工具条
        table.on('tool(assistantUserTable)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if(layEvent === 'delAssistantUser'){ //删除
                layer.confirm('真的要删除么',{icon: 3}, function(index){
                    JsPost("{:url('BookAssistant/assistantUserDel')}",{assistantUserMobile:data.assistantUserMobile}, function (res) {
                        //删除逻辑中判断只有一条数据时应该刷新前一页信息
                        layer.msg(res.msg);
                        if(res.status){
                            if(dataNum == 1){
                                currPage = currPage - 1;
                            }

                            getAssistantInfo(mgrMobile);
                            table.reload('assistantUserTable', {
                                page: {
                                    curr: currPage
                                }
                            });
                            return false;
                        }
                        
                    });
                });
            }
        });

        layui.use('form', function () {
            // 联动助手
            form.on('select(userMobile)', function (data) {
                mgrMobile = data.value;
                getAssistantInfo(mgrMobile);
                table.reload('assistantUserTable', {
                    page: {curr: 1},
                    url: "{:url('BookAssistant/getAssistantList')}?mobile="+data.value,
                });
                return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
            });
        });

        // 联动助手
        function getAssistantInfo(mgrMobile) {
            $.ajax({
                type: 'get',
                url: "{:url('BookAssistant/getAssistantInfo')}",
                data: 'mobile=' + mgrMobile,
                success: function(e) {
                    if(e.status) {
                        $("#assistantUserMobile").empty();//清空下拉框的值
                        $('#assistantUserMobile').append(new Option('请选择小助手', ''));// 下拉菜单里添加元素
                        $.each(e.data, function (index, item) {
                            $('#assistantUserMobile').append(new Option(item.mobile + ' ' + item.truename, item.mobile));// 下拉菜单里添加元素
                        });
                        layui.form.render("select");//重新渲染 固定写法
                    } else {
                        layer.msg(e.msg);
                    }
                }
            });
        };
    })
</script>